﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace TouchControls
{
    public partial class TouchMenuItem
    {
        public event EventHandler MenuOpening;
        public event EventHandler MenuClosing;

        public bool HasItems
        {
            get { return (bool)GetValue(HasItemsProperty); }
            set
            {
                SetValue(HasItemsProperty, value);
                if (value) HasItemsVisibility = Visibility.Visible;
                else HasItemsVisibility = Visibility.Collapsed;
            }
        }

        public virtual Visibility HasItemsVisibility
        {
            get { return (Visibility)GetValue(HasItemsVisibilityProperty); }
            set
            {
                SetValue(HasItemsVisibilityProperty, value);
            }
        }

        public bool IsMenuOpened
        {
            get
            {
                return (bool)GetValue(IsMenuOpenedProperty);
            }
            set
            {
                if (value)
                {
                    if (MenuOpening != null)
                        MenuOpening(this, new EventArgs());
                }
                else
                {
                    if (LineCanvas != null)
                    {
                        LineCanvas.Visibility = Visibility.Hidden;
                    }
                    if (MenuClosing != null)
                        MenuClosing(this, new EventArgs());
                }
                SetValue(IsMenuOpenedProperty, value);
                CheckHostVisibility();
                UpdateVisualStates(false);
                if (!value)
                {
                    foreach (var item in Items)
                    {
                        var menu = item as TouchMenuItem;
                        if (menu.IsMenuOpened)
                            menu.IsMenuOpened = false;
                    }
                }
            }
        }

        private void CheckHostVisibility()
        {
            var host = this.GetTemplateChild("ItemsHost") as FrameworkElement;
            if (host != null)
            {
                if (this.IsMenuOpened)
                {
                    host.Visibility = Visibility.Visible;
                }
                else
                {
                    host.Visibility = Visibility.Collapsed;
                }
            }
        }

        public TouchMenuItem ParentMenu
        {
            get { return (TouchMenuItem)GetValue(ParentTreeProperty); }
            set { SetValue(ParentTreeProperty, value); }
        }

        public object Header
        {
            get { return (object)GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        public double? MenuTileSize
        {
            get
            {
                var res = (double?)GetValue(MenuTileSizeProperty);
                if (!res.HasValue && ParentMenu != null)
                    return ParentMenu.MenuTileSize;

                return res;
            }
            set { SetValue(MenuTileSizeProperty, value); }
        }

        public ControlTemplate ItemButtonTemplate
        {
            get { return (ControlTemplate)GetValue(ItemButtonTemplateProperty); }
            set { SetValue(ItemButtonTemplateProperty, value); }
        }

        public BitmapSource Icon
        {
            get { return (BitmapSource)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }

        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        public object CommandParameter
        {
            get { return (object)GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }



        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CornerRadius.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(TouchMenuItem), null);



        // Using a DependencyProperty as the backing store for CommandParameter.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(TouchMenuItem), null);



        // Using a DependencyProperty as the backing store for Command.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(TouchMenuItem), null);



        // Using a DependencyProperty as the backing store for Icon.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof(BitmapSource), typeof(TouchMenuItem), null);



        // Using a DependencyProperty as the backing store for ItemButtonTemplate.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemButtonTemplateProperty =
            DependencyProperty.Register("ItemButtonTemplate", typeof(ControlTemplate), typeof(TouchMenuItem));



        // Using a DependencyProperty as the backing store for MenuTileSize.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MenuTileSizeProperty =
            DependencyProperty.Register("MenuTileSize", typeof(double?), typeof(TouchMenuItem), null);

        // Using a DependencyProperty as the backing store for Header.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("Header", typeof(object), typeof(TouchMenuItem), null);



        public static readonly DependencyProperty HasItemsProperty =
           DependencyProperty.Register("HasItems", typeof(bool), typeof(TouchMenuItem), null);

        public static readonly DependencyProperty HasItemsVisibilityProperty =
           DependencyProperty.Register("HasItemsVisibility", typeof(Visibility), typeof(TouchMenuItem), null);

        public static readonly DependencyProperty IsMenuOpenedProperty =
            DependencyProperty.Register("IsMenuOpened", typeof(bool), typeof(TouchMenuItem), null);
        public static readonly DependencyProperty ParentTreeProperty =
            DependencyProperty.Register("ParentTree", typeof(TouchMenuItem), typeof(TouchMenuItem), null);
    }
}
